package com.jingyanzi.sys.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jingyanzi.sys.domain.RegionDO;

@Component
public class CityJsonUtil {
	
	@Value("${city.json.path}")
	private String jsonFilePath;
	
	public String getJsonFilePath() {
		return jsonFilePath;
	}

	public void setJsonFilePath(String jsonFilePath) {
		this.jsonFilePath = jsonFilePath;
	}

	public RegionDO getRegionCode(String province, String city, String district) {
		
		InputStream in = null;
		
		BufferedReader jsonIn = null;

		RegionDO regionDO = new RegionDO();
		try {

			JsonParser parser = new JsonParser();
			System.out.println(jsonFilePath);
			jsonIn = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFilePath), "UTF-8"),
					50 * 1024 * 1024);
			JsonObject rootJson = (JsonObject) parser.parse(jsonIn);
			String provinceJson = rootJson.get("86").toString();
			// 查询省级
			ObjectMapper mapper = new ObjectMapper();
			Map<String, String> map = mapper.readValue(provinceJson, Map.class);
			String provCode = getKey(map, province);
			if (provCode == null) {
				return regionDO;
			}
			regionDO.setProvince(provCode);

			// 查询城市
			String cityJson = rootJson.get(provCode).toString();
			mapper = new ObjectMapper();
			map = mapper.readValue(cityJson, Map.class);
			String cityCode = getKey(map, city);
			if (cityCode == null) {
				return regionDO;
			}
			regionDO.setCity(cityCode);

			// 查询地区
			String districtJson = rootJson.get(cityCode).toString();
			mapper = new ObjectMapper();
			map = mapper.readValue(districtJson, Map.class);
			String districtCode = getKey(map, district);
			if (districtCode == null) {
				return regionDO;
			}
			regionDO.setDistrict(districtCode);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				jsonIn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return regionDO;
	}

	public String getKey(Map<String, String> map, String value) {

		String val = "(.*)" + value + "(.*)";
		for (String key : map.keySet()) {
			String v = map.get(key);
			if (v.matches(val) || v.equals("市辖区")) {
				return key;
			}
		}
		return null;
	}

	public RegionDO getRegionDO(String provinceCode, String cityCode, String districtCode) {

		BufferedReader jsonIn = null;
		RegionDO regionDO = new RegionDO();
		CityJsonUtil json = new CityJsonUtil();
		try {

			JsonParser parser = new JsonParser();
			jsonIn = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFilePath), "UTF-8"),
					50 * 1024 * 1024);
			regionDO = new RegionDO();
			JsonObject rootJson = (JsonObject) parser.parse(jsonIn);
			String provinceJson = rootJson.get("86").toString();
			JsonObject pJson = (JsonObject) parser.parse(provinceJson);

			if (StringUtils.isNotEmpty(provinceCode)) {
				JsonElement element = pJson.get(provinceCode);
				regionDO.setProvince(null == element ? null : element.getAsString());
			} else {
				return regionDO;
			}

			if (StringUtils.isNotEmpty(cityCode)) {
				String cityJson = rootJson.get(provinceCode).toString();
				JsonObject cJson = (JsonObject) parser.parse(cityJson);
				if (null != cJson) {
					JsonElement element = cJson.get(cityCode);
					regionDO.setCity(null == element ? null : element.getAsString());
				}
			} else {
				return regionDO;
			}
			if (StringUtils.isNotEmpty(districtCode)) {
				String districtJson = rootJson.get(cityCode).toString();
				JsonObject dJson = (JsonObject) parser.parse(districtJson);
				if (null != dJson) {
					JsonElement element = dJson.get(districtCode);
					regionDO.setDistrict(null == element ? null : element.getAsString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				jsonIn.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return regionDO;
	}

}
